<template>
  <div class="index-lists">
    <el-card class="!border-none" shadow="never">
      <el-form
        ref="formRef"
        class="mb-[-16px]"
        :model="queryParams"
        :inline="true"
      >
        <el-form-item label="用户姓名" prop="name">
          <el-input class="w-56" v-model="queryParams.name" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input class="w-56" v-model="queryParams.mobile" />
        </el-form-item>
        <el-form-item label="园区" prop="zone">
          <el-select v-model="queryParams.zone" placeholder="请选择">
            <el-option label="万科" :value="1"></el-option>
            <el-option label="碧桂园" :value="2"></el-option>
            <el-option label="木栈道" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="折扣" prop="discount">
          <el-input class="w-56" v-model="queryParams.discount" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <div>
        <el-button
          v-perms="['salesman:add']"
          type="primary"
          @click="$router.push('edit'), handleAdd()"
        >
          <template #icon>
            <icon name="el-icon-Plus" />
          </template>
          新增
        </el-button>
      </div>
      <el-table
        class="mt-4"
        size="large"
        v-loading="pager.loading"
        :data="pager.lists"
      >
        <el-table-column label="用户姓名" prop="name" min-width="100" />
        <el-table-column label="手机号" prop="mobile" min-width="100" />
        <el-table-column label="园区" prop="zone" min-width="100">
          <template #default="{ row }">
            {{ row.zone == 1 ? "万科" : row.zone == 2 ? "碧桂园" : "木栈道" }}
          </template>
        </el-table-column>
        <el-table-column label="折扣" prop="discount" min-width="100">
          <template #default="{ row }"> {{ row.discount }}折 </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" min-width="100" />
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <div>
              <router-link
                v-perms="['salesman:edit']"
                class="mr-2"
                :to="{
                  path: 'edit',
                  query: {
                    id: row.id,
                  },
                }"
              >
                <el-link type="primary" :underline="false">编辑</el-link>
              </router-link>
              <el-link
                v-perms="['salesman:del']"
                type="danger"
                link
                @click="handleDelete(row.id)"
              >
                删除
              </el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>
<script lang="ts" setup>
import { salesmanDelete, salesmanLists } from "@/api/salesman";
import { usePaging } from "@/hooks/usePaging";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";
const editRef = shallowRef<InstanceType<typeof EditPopup>>();
const showEdit = ref(false);
const queryParams = reactive({
  userId: "",
  name: "",
  mobile: "",
  zone: "",
  discount: "",
});

const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: salesmanLists,
  params: queryParams,
});

const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.getDetail(data);
};

const handleDelete = async (id: number) => {
  await feedback.confirm("确定要删除？");
  await salesmanDelete({ id });
  feedback.msgSuccess("删除成功");
  getLists();
};

getLists();
</script>
